----------------------
-- Camera Class
----------------------

Camera = {}
Camera.__index = Camera

function Camera.new(w, h)
	local c = {}
	setmetatable(c, Camera)
	
	-- variables
	c.x = 0.0
	c.y = 0.0
	c.width = w
	c.height = h
	c.target_speed = 0.05
    
    -- dead zone
    c.dead_zone_width = 25
    c.dead_zone_height = 25
    
    -- effects
    c.quake_effect = QuakeEffect.new()
    
    -- read only
    c.clamp_x = 0
    c.clamp_y = 0
    c.clamp_width = 0
    c.clamp_height = 0
	
	return c
end

-- update effects like quake
function Camera:update()
    self.quake_effect:update()
end

----------------------
-- Sets area size, the
-- size of the whole level
----------------------
function Camera:setAreaSize(w, h)
	self.width = w
	self.height = h
end

----------------------
-- Clamps the area
----------------------
function Camera:clampArea(bw, bh, w, h)
    self.clamp_x = bw
    self.clamp_y = bh
    self.clamp_width = w
    self.clamp_height = h

	if (self.x + self.width) > (bw + w) then
		self.x = w + bw - self.width
	end
	if (self.y + self.height) > (bh + h) then
		self.y = h + bh - self.height
	end
	
	if self.x < bw then
		self.x = bw
	end
	if self.y < bh then
		self.y = bh
	end
end

----------------------
-- Locks to target
----------------------
function Camera:lockToTarget(x, y)
    local x_temp = x - (self.width * 0.5)
    local y_temp = y - (self.height * 0.5)
    local x_dif = (self.x - x_temp)
    local y_dif = (self.y - y_temp)
    
    if math.abs(x_dif) > self.dead_zone_width then
        self.x = x_temp + (self.dead_zone_width * isPlusMinus(x_dif))
    else
        self.x = linearInterp(self.x, x_temp, 
            self.target_speed, 0.1)        
    end
    
    if math.abs(y_dif) > self.dead_zone_height then
        self.y = y_temp + (self.dead_zone_height * isPlusMinus(y_dif))
    else
        self.y = linearInterp(self.y, y_temp, 
            self.target_speed, 0.1)
    end
end

----------------------
-- Instant lock to target 
----------------------
function Camera:instantLockToTarget(x, y)
    self.x = x - (self.width * 0.5)
    self.y = y - (self.height * 0.5)
end

----------------------
-- Gets render point
-- returns x and y
----------------------
function Camera:getRenderPosition(x, y)
	local x_fix = self:getRenderX(x)
	local y_fiy = self:getRenderY(y)
	
	return x_fix, y_fiy
end

----------------------
-- Gets render position x
----------------------
function Camera:getRenderX(x)
	return (x - self.x + self.quake_effect.x)
end

----------------------
-- Gets render position y
----------------------
function Camera:getRenderY(y)
	return (y - self.y + self.quake_effect.y)
end

----------------------
-- Get Camera position
----------------------
function Camera:getPosition()
    local right = self.x + self.width
    local bottom = self.y + self.height
    
    return self.x, self.y, right, bottom
end
